package cn.evun.tms.generator.task;

import cn.evun.tms.generator.Constants;
import cn.evun.tms.generator.config.Configuration;
import cn.evun.tms.generator.framework.AbstractApplicationTask;
import cn.evun.tms.generator.framework.context.ApplicationContext;
import cn.evun.tms.generator.handler.BaseHandler;
import cn.evun.tms.generator.handler.impl.MapperHandler;
import cn.evun.tms.generator.model.MapperInfo;

import java.util.List;

public class MapperTask extends AbstractApplicationTask {

	private static String MAPPER_FTL = Configuration.getString("base.database")
			.equals(Constants.DB_ORACLE) ? "template/Mapper_oracle.ftl" : "template/Mapper.ftl";

	@SuppressWarnings("unchecked")
	@Override
	protected boolean doInternal(ApplicationContext context) throws Exception {
		logger.info("开始生成Mapper");

		List<MapperInfo> list = (List<MapperInfo>) context.getAttribute("mapperInfos");

		BaseHandler<MapperInfo> handler = null;
		for (MapperInfo mapperInfo : list) {
			handler = new MapperHandler(MAPPER_FTL, mapperInfo);
			handler.execute(context);
		}

		logger.info("生成Mapper完成");
		return false;
	}

	@Override
	protected void doAfter(ApplicationContext context) throws Exception {
		super.doAfter(context);
	}
}
